package com.zhiy.com.estation.ui;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.zhiy.com.estation.R;
import com.zhiy.com.estation.fragmentTag.FragmentTag;
import com.zhiy.com.estation.manager.PreferenceManager;
import com.zhiy.com.estation.ui.base.BaseActivity;
import com.zhiy.com.estation.ui.fragment.Fragment_Friend;
import com.zhiy.com.estation.ui.fragment.Fragment_Life;
import com.zhiy.com.estation.ui.fragment.Fragment_Login;
import com.zhiy.com.estation.ui.fragment.Fragment_Me;
import com.zhiy.com.estation.ui.fragment.Fragment_Msg;
import com.zhiy.com.estation.ui.fragment.Frampus_Campus;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    private Fragment_Friend mFragment_friend;
    private Frampus_Campus mFrampusCampus;
    private Fragment_Me mFragment_me;
    private Fragment_Msg mFragment_msg;
    private Fragment_Life mFragment_life;
    private Fragment mFragment;
    FragmentManager manager;
    FragmentTransaction transaction;
    @BindView(R.id.tab_campus)
    View mCurView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }
    @Override
    protected void initView() {
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        mFrampusCampus = new Frampus_Campus();
        mFragment = mFrampusCampus;
        transaction.add(R.id.myFLayout, mFrampusCampus, FragmentTag.CAMPUS);
        transaction.commit();
        mCurView.setSelected(true);
    }
    @OnClick({R.id.tab_campus, R.id.tab_life, R.id.tab_friend, R.id.tab_me, R.id.tab_msg})
    public void tab_campus(View view) {
        transaction = manager.beginTransaction();
        transaction.hide(mFragment);
        change(view);
        switch (view.getId()) {
            case R.id.tab_campus:
                if (mFrampusCampus == null) {
                    mFrampusCampus = new Frampus_Campus();
                    transaction.add(R.id.myFLayout, mFrampusCampus, FragmentTag.CAMPUS);
                } else {
                    transaction.show(mFrampusCampus);
                }
                mFragment = mFrampusCampus;
                break;
            case R.id.tab_life:
                if (mFragment_life == null) {
                    mFragment_life = new Fragment_Life();
                    transaction.add(R.id.myFLayout, mFragment_life, FragmentTag.LIFE);
                } else {
                    transaction.show(mFragment_life);
                }
                mFragment = mFragment_life;
                break;
            case R.id.tab_friend:
                if (mFragment_friend == null) {
                    mFragment_friend = new Fragment_Friend();
                    transaction.add(R.id.myFLayout, mFragment_friend, FragmentTag.FRAGMENT);
                } else {
                    transaction.show(mFragment_friend);
                }
                mFragment = mFragment_friend;
                break;
            case R.id.tab_msg:
                if (mFragment_msg == null) {
                    mFragment_msg = new Fragment_Msg();
                    transaction.add(R.id.myFLayout, mFragment_msg, FragmentTag.MSG);
                } else {
                    transaction.show(mFragment_msg);
                }
                mFragment = mFragment_msg;
                break;
            case R.id.tab_me:
                if (mFragment_me == null) {
                    mFragment_me = new Fragment_Me();
                    transaction.add(R.id.myFLayout, mFragment_me, FragmentTag.ME);
                } else {
                    transaction.show(mFragment_me);
                }
                mFragment = mFragment_me;
                break;
        }
        transaction.commit();
    }

    public void change(View view) {
        if (mCurView != null) {
            mCurView.setSelected(false);
        }
        view.setSelected(true);
        mCurView = view;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mFragment_me.onActivityResult(requestCode, resultCode, data);
    }
}
